<?php
namespace app\control\controller;

use app\common\model\AdminMenuModel;
use app\common\model\ArticleMessageModel;
use app\common\model\ArticleModel;
use app\common\model\MemberModel;
use app\common\model\OrderModel;
use app\common\model\UserCarMessageModel;
use app\common\model\UserCarModel;
use app\common\model\UserRefundModel;
use think\Db;


class IndexController extends AdminBaseController
{
    public function index()
    {
        $menu=AdminMenuModel::getMenu(0,$this->userInfo)["tree"];
        $this->assign("Menu",$menu);

        return $this->fetch("index/index");
    }

    public function welcome()
    {
        $data=$this->count_data();

        $this->assign('count',$data);
        $this->assign('sys_info',$this->get_sys_info());
        return $this->fetch();
    }

    /**
     * 获取器系统信息
     * @return mixed
     */
    public function get_sys_info(){
        $sys_info['os']             = PHP_OS;
        $sys_info['zlib']           = function_exists('gzclose') ? 'YES' : 'NO';//zlib
        $sys_info['safe_mode']      = (boolean) ini_get('safe_mode') ? 'YES' : 'NO';//safe_mode = Off
        $sys_info['timezone']       = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "no_timezone";
        $sys_info['curl']			= function_exists('curl_init') ? 'YES' : 'NO';
        $sys_info['web_server']     = $_SERVER['SERVER_SOFTWARE'];
        $sys_info['phpv']           = phpversion();
        $sys_info['ip'] 			= GetHostByName($_SERVER['SERVER_NAME']);
        $sys_info['fileupload']     = @ini_get('file_uploads') ? ini_get('upload_max_filesize') :'unknown';
        $sys_info['max_ex_time'] 	= @ini_get("max_execution_time").'s'; //脚本最大执行时间
        $sys_info['set_time_limit'] = function_exists("set_time_limit") ? true : false;
        $sys_info['domain'] 		= $_SERVER['HTTP_HOST'];
        $sys_info['memory_limit']   = ini_get('memory_limit');
        $sys_info['version']   	    =\think\App::VERSION;
        $mysqlinfo = Db::query("SELECT VERSION() as version");
        $sys_info['mysql_version']  = $mysqlinfo[0]['version'];
        if(function_exists("gd_info")){
            $gd = gd_info();
            $sys_info['gdinfo'] 	= $gd['GD Version'];
        }else {
            $sys_info['gdinfo'] 	= "未知";
        }
        return $sys_info;
    }

    /**
     * 数量统计
     * @return array
     */
    public function count_data(){
        //已支付订单数量
        $orderCount=OrderModel::where('status',OrderModel::SHOW_STATUS)
            ->count();

        //用户数量
        $userCount=MemberModel::where('status','>',MemberModel::STATUS_DEL)
            ->count();

        //今日新增用户数量
        $today=strtotime(date('Y-m-d 00:00:00'));
        $userNowCount=MemberModel::where('status','>',MemberModel::STATUS_DEL)
            ->where('create_time','>=',$today)
            ->count();

        //待服务订单
        $svrCount=OrderModel::where('status',OrderModel::HIDE_STATUS)
            ->where('order_type',OrderModel::TYPE_NO)
            ->count();

        //用户发布车辆
        $carCount=UserCarModel::where('status',UserCarModel::SHOW_STATUS)
            ->count();

        //今日用户发布车辆
        $carNowCount=UserCarModel::where('status',UserCarModel::SHOW_STATUS)
            ->where('adopt_time','>=',$today)
            ->count();

        //用户发布待审核车辆
        $carSHCount=UserCarModel::where('status',UserCarModel::HIDE_STATUS)
            ->count();

        //待审核车辆评论
        $carPLCount=UserCarMessageModel::where('status',UserCarMessageModel::HIDE_STATUS)
            ->count();

        //待审核文章评论
        $articlePLCount=ArticleMessageModel::where('status',ArticleMessageModel::HIDE_STATUS)
            ->count();

        //用户提现申请
        $userRefund=UserRefundModel::where('status',UserRefundModel::HIDE_STATUS)
            ->count();

        //文章数量
        $articleCount=ArticleModel::where('status',ArticleModel::SHOW_STATUS)
            ->count();

        //当天开始时间
        $start_time=strtotime(date("Y-m-d",time()));
        //当天结束之间
        $end_time=$start_time+60*60*24;

        //今日新增发布车辆
        $dayCarCount=UserCarModel::where('status',UserCarModel::SHOW_STATUS)
            ->where('create_time','>=',$start_time)
            ->where('create_time','<=',$end_time)
            ->count();
        //今日新增车辆留言
        $dayCarMsgCount=UserCarMessageModel::where('status',UserCarMessageModel::SHOW_STATUS)
            ->where('create_time','>=',$start_time)
            ->where('create_time','<=',$end_time)
            ->count();
        //今日新增发布车辆
        $dayArticleCount=ArticleMessageModel::where('status',ArticleMessageModel::SHOW_STATUS)
            ->where('create_time','>=',$start_time)
            ->where('create_time','<=',$end_time)
            ->count();

        $data=[
            'orderCount'=>$orderCount,
            'userCount'=>$userCount,
            'userNowCount'=>$userNowCount,
            'svrCount'=>$svrCount,
            'carCount'=>$carCount,
            'carNowCount'=>$carNowCount,
            'carSHCount'=>$carSHCount,
            'userRefund'=>$userRefund,
            'articleCount'=>$articleCount,
            'carPLCount'=>$carPLCount,
            'articlePLCount'=>$articlePLCount,
            'dayCarCount'=>$dayCarCount,
            'dayCarMsgCount'=>$dayCarMsgCount,
            'dayArticleCount'=>$dayArticleCount,
        ];

        return $data;
    }
}
